import dom, { ajax, domg } from "../../init.js";
import { obj } from "../../index.js";
import { date } from "../../../util/date.js";
import { commemt } from "../../../util/interface.js";
import { initcomment } from "./comment.js";

export default {
  async clickSubmit() {
    const content = (domg("#commentcontent") as any).value.trim();
    if (!content) return alert("请输入内容");
    const createdTime = date("all");
    const result = await ajax.post("/comment/submit", {
      content,
      createdTime,
    });
    const data = JSON.parse(result as string) as {
      status: number;
      msg: string;
      data?: commemt;
    };
    if (data.msg !== "ok") return alert("发布失败," + data.msg);
    initcomment();
    return alert("发布成功");
  },
  async zancai(this: any, e: Event) {
    try {
      let dev = (e.target as HTMLElement).parentElement?.getAttribute(
        "data-dev"
      );

      if (e.target === domg(`ul>li:nth-of-type(${dev}) .zanicon`)) {
        if (
          domg(`ul>li:nth-of-type(${dev}) .zanicon`).classList.length >= 4 ||
          domg(`ul>li:nth-of-type(${dev}) .caiicon`).classList.length >= 4
        ) {
          return alert("无法修改投票结果");
        }
        let id = domg(
          `ul>li:nth-of-type(${dev}) .zanicon`
        ).parentElement?.getAttribute("data-index");
        if (!id) return alert("id不存在");
        if (+id === NaN) return alert("id类型错误");
        const result = await ajax.post("/comment/update", {
          zan: "zan",
          id,
        });
        const is = JSON.parse(result as string) as any;
        if (!is || is.msg !== "ok") return alert("点赞失败");
        localStorage.setItem(`commentis${id}`, "1");
        domg(`ul>li:nth-of-type(${dev}) .zanicon`).classList.add("iszancai");
        domg(`ul>li:nth-of-type(${dev}) .caiicon`).classList.add("nozancai");
        let count = domg(`ul>li:nth-of-type(${dev}) .zan`).innerText;
        count = String(+String(count) + 1);
        domg(`ul>li:nth-of-type(${dev}) .zan`).innerText = count;
        console.log("点赞成功");
      } else if (e.target === domg(`ul>li:nth-of-type(${dev}) .caiicon`)) {
        console.log(2);

        if (
          domg(`ul>li:nth-of-type(${dev}) .caiicon`).classList.length >= 4 ||
          domg(`ul>li:nth-of-type(${dev}) .zanicon`).classList.length >= 4
        ) {
          return alert("无法修改投票结果");
        }
        let id = domg(
          `ul>li:nth-of-type(${dev}) .caiicon`
        ).parentElement?.getAttribute("data-index");
        if (!id) return alert("id不存在");
        if (+id === NaN) return alert("id类型错误");
        const result = await ajax.post("/comment/update", {
          cai: "cai",
          id,
        });
        const is = JSON.parse(result as string) as any;
        if (!is || is.msg !== "ok") return alert("点踩失败");
        localStorage.setItem(`commentis${id}`, "2");
        domg(`ul>li:nth-of-type(${dev}) .caiicon`).classList.add("iszancai");
        domg(`ul>li:nth-of-type(${dev}) .zanicon`).classList.add("nozancai");
        let count = domg(`ul>li:nth-of-type(${dev}) .zan`).innerText;
        count = String(+String(count) + 1);
        domg(`ul>li:nth-of-type(${dev}) .cai`).innerText = count;
        console.log("点踩成功");
      }
    } catch (error) {
      console.log("no");
    }
  },
};
